package com.yuncang.base.loginserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncang.base.loginserver.entity.po.User;
import com.yuncang.base.loginserver.service.UserService;
import com.yuncang.common.entity.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzb
 * @since 2023-09-15
 */
@RestController
@RequestMapping("/loginserver/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("query")
    public ApiResponse<List<User>> queryUser(){
        return ApiResponse.success(userService.lambdaQuery().list());
    }

    @GetMapping("queryUserById/{id}")
    public ApiResponse<User> queryUserById(@PathVariable("id") Long id){
        return ApiResponse.success(userService.lambdaQuery().eq(User::getId,id).one());
    }

    @PostMapping("insert")
    public ApiResponse<Boolean> inert(@RequestBody User user){
        return ApiResponse.success(userService.save(user));
    }

    @PostMapping("update")
    public ApiResponse<Void> update(){

        userService.lambdaUpdate()
                .eq(User::getId,"1")
                .set(User::getUserName,"李四");
        return ApiResponse.success();
    }

    @PostMapping("deleted/{id}")
    public ApiResponse<Boolean> deleted(@PathVariable("id") Long id){

        return ApiResponse.success(userService.removeById(id));
    }

    @GetMapping("test/{type}")
    public ApiResponse<List<User>> test(@PathVariable("type") String userType){

        return ApiResponse.success(userService.test(userType));
    }


    @GetMapping("page1")
    public ApiResponse<IPage<User>> page1(){
        IPage<User> user = new Page<>(1,10);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name","张");
        return ApiResponse.success(userService.page(user,queryWrapper));
    }
}

